cc.Class({
  extends: cc.Component,

  properties: {
    // 完整排行榜显示
    RankingListView: cc.Node,
    content: cc.Node,
    myInfoNode: cc.Node,
    UserInfoItem: cc.Prefab,
    UserInfoList: null,
  },

  start() {
    let self = this;

    // 接受主域传来的数据
    wx.onMessage(data => {
      // 销毁原有排行榜数据
      self.content.destroyAllChildren();

      if (data.key == 'getFriendData') {
        self.getFriendData();
      } else if (data.openid) {
        self.showRankingData(data.openid);
      }
    });

  },

  // 获取好友托管数据
  getFriendData() {
    let self = this;
    wx.getFriendCloudStorage({
      keyList: ["max_score"],
      success: function (res) {
        self.UserInfoList = self.sortList(res.data, false);
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 展示总排行数据
  showRankingData(openid) {
    let self = this;
    this.UserInfoList.forEach((user, index) => {
      // 如果和自己的openid一样,则显示自己的数据
      if (user.openid == openid) {
        self.showMyInfo(user, index);
      }
      if (!self.preSettingData(index, user, self.UserInfoItem, self.content)) {
        return;
      }
    });
  },

  // 展示自己数据方法
  showMyInfo(user, index) {
    let userRanking = this.myInfoNode.getChildByName('userRanking').getComponent(cc.Label);
    let userIcon = this.myInfoNode.getChildByName('mask').children[0].getComponent(cc.Sprite);
    let userName = this.myInfoNode.getChildByName('userName').getComponent(cc.Label);
    let userScore = this.myInfoNode.getChildByName('userScore').getComponent(cc.Label);

    if (user.KVDataList.length == 0) return;

    userRanking.string = index + 1;
    userName.string = user.nickname;
    userScore.string = user.KVDataList[0].value;

    cc.loader.load({
      url: user.avatarUrl, type: 'png'
    }, (err, texture) => {
      if (err) console.error(err);
      userIcon.spriteFrame = new cc.SpriteFrame(texture);
    });
  },

  // 前置处理用户数据
  preSettingData(index, user, prefab, content) {
    if (!user) {
      return false;
    }
    if (user.KVDataList.length == 0) return;
    let nickName = user.nickname;
    let avatarUrl = user.avatarUrl;
    let ranking = index + 1;
    let max_score = user.KVDataList[0].value;
    this.showUserData(ranking, nickName, avatarUrl, max_score, prefab, content);
    return true;
  },

  // 展示好友数据方法
  showUserData(ranking, nickName, avatarUrl, max_score, prefab, content) {
    let infoItem = cc.instantiate(prefab); // 实例化一个用户信息item
    infoItem.parent = content;
    let userRanking = infoItem.getChildByName('userRanking').getComponent(cc.Label);
    let userIcon = infoItem.getChildByName('mask').children[0].getComponent(cc.Sprite);
    let userName = infoItem.getChildByName('userName').getComponent(cc.Label);
    let userScore = infoItem.getChildByName('userScore').getComponent(cc.Label);

    userRanking.string = ranking;
    userName.string = nickName;
    userScore.string = max_score;

    cc.loader.load({
      url: avatarUrl, type: 'png'
    }, (err, texture) => {
      if (err) console.error(err);
      userIcon.spriteFrame = new cc.SpriteFrame(texture);
    });

    console.log(nickName + '\'s info has been getten.');
  },

  // 排序方法 (ListData：res.data; order:false降序，true升序)
  sortList: function (ListData, order) {
    ListData.sort(function (a, b) {
      let AMaxScore = 0;
      let BMaxScore = 0;
      let list = a.KVDataList;

      // 获取a的最大分数
      for (let i = 0; i < list.length; i++) {
        if (list[i].key == "max_score") {
          AMaxScore = list[i].value;
        }
      }

      list = b.KVDataList;
      // 获取b的最大分数
      for (let i = 0; i < list.length; i++) {
        if (list[i].key == "max_score") {
          BMaxScore = list[i].value;
        }
      }

      // 排序
      if (order) {
        return parseInt(AMaxScore) - parseInt(BMaxScore);
      } else {
        return parseInt(BMaxScore) - parseInt(AMaxScore);
      }
    });

    return ListData;
  },

});
